package com.fumin.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.UUID;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

@Configuration
public class configure {
    private static String UPDATER_ID="0001";
    private static String UPDATER_GROUP="updaters";


    @Bean(name = "scheduler")
    public Scheduler getScheduler() throws SchedulerException, ParseException {
        Scheduler scheduler =  StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();
//        addAutoControlJob(scheduler);
        return scheduler;
    }




    public static void addAutoControlJob(Scheduler scheduler) throws ParseException, SchedulerException {
        JobDetail job = newJob(ClassesUpdater.class).
                withIdentity(UUID.randomUUID().toString(), UPDATER_GROUP).
                build();
        CronExpression cronExpression = new CronExpression("0 */2 * * * ? *");
        Trigger trigger=null;
        trigger = newTrigger()
                    .withIdentity(UUID.randomUUID().toString(), UPDATER_GROUP)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)
                    ).build();
        scheduler.scheduleJob(job, trigger);
    }

}
